import { createStore } from 'vuex'
import axios from 'axios'

export default createStore({
  state: {
    tasks: [],
    tasksByQuadrant: {
      1: [], // 重要且紧急
      2: [], // 重要不紧急
      3: [], // 不重要但紧急
      4: []  // 不重要不紧急
    }
  },
  mutations: {
    SET_TASKS(state, tasks) {
      console.log('SET_TASKS 被调用，赋值：', tasks)
      state.tasks = Array.isArray(tasks) ? tasks : []
      console.log('SET_TASKS 执行后，state.tasks:', state.tasks)
    },
    SET_TASKS_BY_QUADRANT(state, { quadrant, tasks }) {
      state.tasksByQuadrant[quadrant] = tasks
    },
    UPDATE_TASK_STATUS(state, { taskId, status }) {
      const task = state.tasks.find(t => t.id === taskId)
      if (task) {
        task.status = status
      }
    },
    SET_ALL_TASKS_BY_QUADRANT(state, newData) {
      Object.keys(newData).forEach(key => {
        state.tasksByQuadrant[String(key)] = newData[key]
      })
    }
  },
  actions: {
    async fetchTasks({ commit }) {
      try {
        const response = await axios.get('/zwart/user/task/list')
        console.log('任务接口返回:', response.data)
        commit('SET_TASKS', response.data)
      } catch (error) {
        console.error('Error fetching tasks:', error)
      }
    },
    async fetchTasksByQuadrant({ commit }) {
      try {
        const response = await axios.get('/zwart/user/task/partition')
        console.log('接口返回数据:', response.data)
        if (response.data.code === 200) {
          const rawData = response.data.data
          const normalized = { '1': [], '2': [], '3': [], '4': [] }
          Object.keys(rawData).forEach(key => {
            if (['1','2','3','4'].includes(key)) {
              normalized[key] = rawData[key]
            }
          })
          commit('SET_ALL_TASKS_BY_QUADRANT', normalized)
        } else {
          console.error('API返回错误:', response.data)
        }
      } catch (error) {
        console.error('Error fetching tasks by quadrant:', error.response || error)
      }
    },
    async completeTask({ dispatch }, taskId) {
      try {
        const response = await axios.post('http://localhost:8080/zwart/user/task/complete', { taskId })
        if (response.data.code === 0) {
          await dispatch('fetchTasks')
        }
      } catch (error) {
        console.error('Error completing task:', error)
      }
    },
    async cancelTask({ dispatch }, taskId) {
      try {
        const response = await axios.post('http://localhost:8080/zwart/user/task/cancel', { taskId })
        if (response.data.code === 0) {
          await dispatch('fetchTasks')
        }
      } catch (error) {
        console.error('Error canceling task:', error)
      }
    },
    async deleteTask({ dispatch }, taskId) {
      try {
        const response = await axios.delete('http://localhost:8080/zwart/user/task/delete', { data: { taskId } })
        if (response.data.code === 0) {
          await dispatch('fetchTasks')
        }
      } catch (error) {
        console.error('Error deleting task:', error)
      }
    },
    async updateTask({ dispatch }, task) {
      try {
        const response = await axios.post('http://localhost:8080/zwart/user/task/update', task)
        if (response.data.code === 0) {
          await dispatch('fetchTasks')
        }
      } catch (error) {
        console.error('Error updating task:', error)
      }
    },
    async restoreTask({ dispatch }, taskId) {
      try {
        const response = await axios.post('http://localhost:8080/zwart/user/task/restore', { taskId })
        if (response.data.code === 0) {
          await dispatch('fetchTasks')
        }
      } catch (error) {
        console.error('Error restoring task:', error)
      }
    }
  }
}) 